package com.mdp.workflow.client.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdp.core.service.BaseService;
import com.mdp.core.utils.CollectionUtils;
import com.mdp.safe.client.entity.Dept;
import com.mdp.workflow.client.entity.WfSysUser;
import com.mdp.workflow.client.mapper.WfSysUserMapper;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.mdp.core.utils.CollectionUtils.convertMap;
import static com.mdp.core.utils.CollectionUtils.convertSet;

/**
 * 父类已经支持增删改查操作,因此,即使本类什么也不写,也已经可以满足一般的增删改查操作了.<br> 
 * 组织 com.qqkj  顶级模块 oa 大模块 hr.user 小模块 <br>
 * 实体 SysUser 表 ADM.sys_user 当前主键(包括多主键): userid;
 *
 * 如果多数据源切换有问题，看这便文章 https://blog.csdn.net/weixin_42556307/article/details/136486514
 ***/
@DS("adm-ds")
@Service("mdp.workflow.pub.sysUserService")
public class WfSysUserService extends BaseService<WfSysUserMapper, WfSysUser> {


    /**
     * 请在此类添加自定义函数
     */

    public Set<String> selectUseridsByPostIds(Collection<String> postIds){
        return convertSet(baseMapper.selectUseidsByPostIds(postIds));
    }
    public Set<String> selectUseridsByDeptIds(Collection<String> deptids){
        return convertSet(baseMapper.selectUseidsByDeptids(deptids));
    }
    public Set<String> selectUseridsByRoleids(Collection<String> roleids){
        return convertSet(baseMapper.selectUseidsByPostIds(roleids));
    }

    public Map<String,WfSysUser> mapByIds(Collection<String> ids){
        List<WfSysUser> users=listByIds(ids);
        return CollectionUtils.convertMap(users,WfSysUser::getUserid);
    }


    /**
     * 自定义查询，支持多表关联
     * @param page 分页条件
     * @param ew 一定要，并且必须加@Param("ew")注解
     * @param ext 如果xml中需要根据某些值进行特殊处理，可以通过这个进行传递，非必须，注解也可以不加
     * @return
     */
    public List<Map<String,Object>> selectListMapByWhere(IPage page, QueryWrapper ew, Map<String,Object> ext){
        return baseMapper.selectListMapByWhere(page,ew,ext);
    }

    public Dept getDept(String deptid) {
        return  baseMapper.getDept(deptid) ;
    }


    public List<Map<String, String>> selectSimpleUsersByUserids(Collection<String> userids) {
        return baseMapper.selectSimpleUsersByUserids(userids);
    }

    /**
     * 返回 {userid1:username2,userid2:username2,.....}
     * @param userids
     * @return
     */
    public Map<String, String> mapByUserids(Collection<String> userids) {
        return convertMap(baseMapper.selectSimpleUsersByUserids(userids),o->o.get("userid"),o->o.get("username"));
    }

    public List<Dept> selectDeptListByDeptids(Collection<String> depptids) {
        return baseMapper.selectDeptListByDeptids(depptids);
    }
}

